package com.rerun.phone.memberuser;

import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.WorkExperience;
import com.rerun.entity.vo.WorkExpVo;
import com.rerun.service.IWorkExpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

/**
 * @author dax.
 * @version v1.0
 * @since 2017/10/19 15:15
 */
@Api(value = "求职端-工作经历", description = "求职端-工作经历")
@RestController
@RequestMapping("/memberApi/workExp")
public class WorkExpController extends SuperController {
    @Resource
    private IWorkExpService iWorkExpService;

    @ApiOperation(value = "添加工作经历", httpMethod = "POST", response = Rest.class, notes = "添加工作经历")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest add(@ModelAttribute WorkExpVo workExpVo) {
        WorkExperience workExperience = new WorkExperience();
        try {
            BeanUtils.copyProperties(workExperience, workExpVo);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        workExperience.setCreateTime(new Date());
        boolean flag = iWorkExpService.insert(workExperience);
        return flag ? Rest.ok() : Rest.failure("add failure");
    }
    @ApiOperation(value = "修改工作经历", httpMethod = "POST", response = Rest.class, notes = "修改工作经历")
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest update(@ModelAttribute WorkExpVo workExpVo, @PathVariable("id") String id) {
        WorkExperience workExperience = new WorkExperience();
        try {
            BeanUtils.copyProperties(workExperience, workExpVo);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        workExperience.setId(id);
        workExperience.setUpdateTime(new Date());
        boolean flag = iWorkExpService.updateById(workExperience);
        return flag ? Rest.ok() : Rest.failure("update failure");
    }
    /**
     * 通过工作经历ID删除工作经历.
     *
     * @param id the id
     * @return the rest
     */
    @ApiOperation(value = "通过工作经历ID删除工作经历", httpMethod = "POST", response = Rest.class, notes = "通过工作经历ID删除工作经历")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest deleteById(@PathVariable("id") String id) {
        boolean flag = iWorkExpService.deleteById(id);
        return flag ? Rest.ok() : Rest.failure("delete failure");
    }
}
